﻿/*
Copyright (C) 2015 Dieter Vandroemme


Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace RandomUtils.Examples {
    public partial class LargeListExample : Form {
        private LargeListExampleControl[] _range;
        public LargeListExample() {
            InitializeComponent();

            largeList1.SizeMode = SizeMode.StretchHorizontal; //Sizing of the controls.

            int controls = 100; //Try 1000 or 10000, but not from within VS or creating controls will take forever.
            _range = new LargeListExampleControl[controls];
            var sw = Stopwatch.StartNew();
            //Parallel.For(0, controls, (i) => { //This can fail.
            for (int i = 0; i != controls; i++) {
                _range[i] = CreateExampleControl("control " + i);
                //Also possible: largeList1.Add(ctrl, false); Use largeList1.RefreshControls() in the HandleCreated callback.
            }
            //);
            sw.Stop();
            MessageBox.Show("Time to create example controls: " + sw.ElapsedMilliseconds + " millis.");

            //Only for the Stopwatch, to be sure adding controls to the GUI is effectively done. Otherwise not needed. LargeList only adds controls to it's GUI when it has a window handle.
            largeList1.HandleCreated += LargeList1_HandleCreated;
        }

        private void LargeList1_HandleCreated(object sender, EventArgs e) {
            largeList1.HandleCreated -= LargeList1_HandleCreated;

            var sw = Stopwatch.StartNew();
            largeList1.AddRange(_range);
            sw.Stop();
            MessageBox.Show("Time to add the example controls to the GUI: " + sw.ElapsedMilliseconds + " millis.");
        }

        //private Type _exampleControlType = typeof(LargeListExampleControl);
        //If you run this example with a debugger attached (VS) creating controls will be slow.
        //You will notice that this goes a lot faster when running the examples exe directly.
        private LargeListExampleControl CreateExampleControl(string label) {
            var ctrl = new LargeListExampleControl();
            ctrl.Label = label;
            ctrl.Selected += ExampleCtrl_Selected;
            ctrl.Delete += Ctrl_Delete;
            ctrl.Duplicate += Ctrl_Duplicate;
            return ctrl;
        }

        private void Ctrl_Duplicate(object sender, EventArgs e) {
            var ctrl = sender as LargeListExampleControl;
            var newCtrl = CreateExampleControl("new " + ctrl.Label);

            var nextCtrl = largeList1.NextControl(ctrl);
            if (nextCtrl == null) largeList1.Add(newCtrl); else largeList1.Insert(newCtrl, largeList1.IndexOf(nextCtrl));
        }

        private void Ctrl_Delete(object sender, EventArgs e) {
            largeList1.Remove(sender as LargeListExampleControl);
        }

        private void ExampleCtrl_Selected(object sender, EventArgs e) {
            foreach (LargeListExampleControl ctrl in largeList1.AllControls)
                if (ctrl != sender) ctrl.Unfocus();
        }
    }
}
